package com.sunkq.algorithms;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class TreedNodeTest1 {

    public static void main(String[] args) {
        TreedNodeTest1 test = new TreedNodeTest1();
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(test.maxDepth(root));
    }

    @Test
    public void testTraverse(){
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        root.left.left = new TreeNode(18);
        root.left.right = new TreeNode(19);
        root.left.left.left = new TreeNode(22);
        root.left.left.right = new TreeNode(25);
        traverse(root);
        System.out.println(preorder);
        System.out.println(inorder);
        System.out.println(postorder);
    }

    public List<Integer> preorder = new ArrayList<>();

    public List<Integer> inorder = new ArrayList<>();

    public List<Integer> postorder = new ArrayList<>();

    void traverse(TreeNode root) {
        if (root == null) {
            return;
        }
        // 前序遍历
        preorder.add(root.val);
        traverse(root.left);
        inorder.add(root.val);
        traverse(root.right);
        postorder.add(root.val);
    }


    // 二叉树遍历函数
    void traverse(TreeNode root, int level) {
        if (root == null) {
            return;
        }
        // 前序位置
        System.out.printf("节点 %s 在第 %d 层", root.val, level);
        traverse(root.left, level + 1);
        traverse(root.right, level + 1);
    }

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftMax = maxDepth(root.left);
        int rightMax = maxDepth(root.right);
        return Math.max(leftMax, rightMax) + 1;
    }

    @Test
    public void testMaxDepth(){
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(maxDepth(root));
    }
}

